package org.jeecg.modules.equipment.gasmeter.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: scott
 * @Date: 2019-04-03
 */
public class BulletUtil {


	/**
	 *
	 * @param maxCode 当前最大编号
	 * @param prefix 前缀
	 * @return 下个编号
	 */
	public static String nextCode(String maxCode,String prefix) {

		String code = prefix+"001";
		try {
			//获取当前最大编号
			if(StringUtils.isNoneEmpty(maxCode)){
				code = String.valueOf(Integer.parseInt(maxCode.substring(prefix.length()))+1);
				for (int i = 0; i < 3; i++){
					code = code.length() < 3 ? "0" + code : code;
				}
				code=prefix+code;

			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return code;
	}


	/**
	 *
	 * @param maxCode 当前最大编号
	 * @param prefix 前缀
	 * @param num 生成几位数字
	 * @return 下个编号
	 */
	public static String nextCode(String maxCode,String prefix,int num) {

		 String code = "1";
		try {
				//获取当前最大编号
				if(StringUtils.isNoneEmpty(maxCode)){
				code = String.valueOf(Integer.parseInt(maxCode.substring(prefix.length()))+1);
				}

					for (int i = 0; i < num; i++){
						code = code.length() < num ? "0" + code : code;
					}
					   code=prefix+code;
		} catch (NumberFormatException e) {
			e.printStackTrace();

		}
		return code;
	}


	public static String getTenantId(HttpServletRequest req) {
		String tenantId = "-1";
		try {
			 tenantId = req.getHeader("tenant-id");
		} catch (Exception e) {
			System.out.println("未获取到tenantId");
		}
		return tenantId;
	}


	
}
